﻿using System.Collections.Generic;
using System.Linq;
using Basic4Net.Entities;

namespace Basic4Net.BusinessRules
{
    public class Position : BaseBR
    {
        #region 增删改
        /// <summary>
        /// 添加新职称
        /// </summary>
        /// <param name="entity">职称实体</param>
        /// <param name="msg">错误信息</param>
        /// <returns>是否成功</returns>
        public bool Add(t_Position entity, ref string msg)
        {
            bool result = true;
            DBEntities db;
            using (db = new DBEntities())
            {
                //效验重名
                int checkName = (from b in db.t_Position
                                 where b.FName == entity.FName
                                 select b).Count();
                if (checkName > 0)
                {
                    msg = "已经存在名称为【" + entity.FName + "】的职称.";
                    result = false;
                }

                if (result)
                {
                    db.AddTot_Position(entity);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        result = false;
                        msg = "操作失败！";
                    }
                }
                return result;
            }
        }

        /// <summary>
        /// 修改职称
        /// </summary>
        /// <param name="entity">职称实体</param>
        /// <param name="msg">错误信息</param>
        /// <returns>是否成功</returns>
        public bool Update(t_Position entity, ref string msg)
        {
            bool result = true;

            DBEntities db;
            using (db = new DBEntities())
            {
                //效验重名
                int checkName = (from b in db.t_Position
                                 where b.FName == entity.FName && b.RID != entity.RID
                                 select b).Count();
                if (checkName > 0)
                {
                    msg = "已经存在名称为【" + entity.FName + "】的职称.";
                    result = false;
                }

                if (result)
                {
                    db.t_Position.FirstOrDefault(p => p.RID == entity.RID);
                    db.ApplyCurrentValues("t_Position", entity);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        result = false;
                        msg = "操作失败！";
                    }
                }
                return result;
            }
        }

        /// <summary>
        /// 删除职称
        /// </summary>
        /// <param name="entity">职称实体</param>
        /// <param name="msg">错误信息</param>
        /// <returns>是否成功</returns>
        public bool Delete(t_Position entity, ref string msg)
        {
            bool result = true;
            DBEntities db;
            using (db = new DBEntities())
            {
                try
                {
                    t_Position pos = db.t_Position.FirstOrDefault(p => p.RID == entity.RID);
                    db.t_Position.DeleteObject(pos);
                    db.SaveChanges();
                }
                catch
                {
                    result = false;
                    msg = "操作失败！";
                }
                return result;
            }
        }
        #endregion

        /// <summary>
        /// 获取所有职称
        /// </summary>
        /// <returns></returns>
        public IList<t_Position> GetAllPosition()
        { 
            DBEntities db;
            using (db = new DBEntities())
            {
                var list = (from postion in db.t_Position
                               select postion);

                IList<t_Position> resList = list.ToList();

                return resList;
            }
        }
    }
}
